#-------------------------------------------------------------------------------
# Name:        ResolutionSlider
# Purpose:
#
# Author:      Pablo
#
# Created:     10/07/2012
# Copyright:   (c) Monxcleyr Productions 2012
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os
from ..gui import button, label

class ResSlider():

  def __init__(self, pos, currentsize, fullscreen, surface, sound, soundfile, common, debug, title, font):
    self.pos = pos
    self.current = currentsize[0]
    self.common = common
    self.index = self.common.reswidths.index(self.current)
    self.fullscreen = fullscreen
    self.surface = surface
    self.sound = sound
    self.soundfile = soundfile
    self.debug = debug
    self.title = title
    self.font = font
    self.box = pygame.image.load(os.path.join('resources/graphics/options/volumebox.bmp'))
    self.arrow = pygame.image.load(os.path.join('resources/graphics/options/volumearrow.bmp'))
    self.arrowhover = pygame.image.load(os.path.join('resources/graphics/options/volumearrowhover.bmp'))
    self.box.set_colorkey((255,0,255))
    self.arrow.set_colorkey((255,0,255))
    self.arrowhover.set_colorkey((255,0,255))
    self.arrowleft = pygame.transform.rotate(self.arrow, 180)
    self.arrowlefthover = pygame.transform.rotate(self.arrowhover, 180)

    total = self.arrow.get_width() + 1 + self.box.get_width() + 1 + self.arrow.get_width()
    total = total / 2
    self.pos = (self.pos[0] - total, self.pos[1])

    self.arrowpos = self.pos[0] + self.box.get_width() + self.arrow.get_width() + 2
    self.boxpos = self.pos[0] + self.arrow.get_width() + 1
    self.arrowrect = self.getRect(self.arrowpos, self.arrow)
    self.arrowleftrect = self.getRect(self.pos[0], self.arrow)
    self.blockpos = self.boxpos + 3

    boxheight = self.box.get_height()
    self.mutebutton = button.Checkbox(self.surface, (self.boxpos, self.pos[1] + boxheight + 3), self.font, '')
    if self.fullscreen == True:
      self.mutebutton.setState(1)
    self.label = label.Label(self.surface, 'Fullscreen', (self.boxpos + 22, self.pos[1] + boxheight + 4), 'left', self.font, (1, 106, 251))
    self.title = label.Label(self.surface, self.title, (self.boxpos, self.pos[1] - 13), 'left', self.font, (1, 106, 251))
    self.reslabel = label.Label(self.surface, '', ((self.pos[0] + self.arrow.get_width() + 1 + self.box.get_width() / 2 ), \
      self.pos[1] + 3), 'center', self.font, (1, 106, 251))

    self.upclicked, self.downclicked = False, False

  def draw(self, mouse):

    self.surface.blit(self.box, (self.boxpos, self.pos[1]))
    self.surface.blit(self.arrowleft, self.pos)
    self.surface.blit(self.arrow, (self.arrowpos, self.pos[1]))

    # Draw hovered buttons
    if self.arrowrect.collidepoint(mouse[0]):
      self.surface.blit(self.arrowhover, (self.arrowpos, self.pos[1]))
      if mouse[1] == 1:
        self.upclicked = True
        self.sound.play(self.soundfile, self.common.settings)
    if self.arrowleftrect.collidepoint(mouse[0]):
      self.surface.blit(self.arrowlefthover, self.pos)
      if mouse[1] == 1:
        self.sound.play(self.soundfile, self.common.settings)
        self.downclicked = True


    # Change resolution when arrows are clicked
    if self.upclicked == True and self.index != len(self.common.reswidths) - 1:
      self.index += 1
      self.debug.add('Res Index: ' + str(self.index))
    if self.downclicked == True and self.index != 0:
      self.index -= 1
      self.debug.add('Res Index: ' + str(self.index))
    self.upclicked, self.downclicked = False, False


    # Draw resolution
    self.current = self.common.reswidths[self.index]
    width = int(self.current / self.common.scale)
    self.reslabel.setText(str(self.current) + ' x ' + str(width))

# Use for fullscreen button later
    if mouse[1] == 1:
      temp = 0
    else:
      temp = 1
    self.mutebutton.draw(mouse[0], mouse[1], temp)

    if self.mutebutton.state() == 1:
      if self.fullscreen == False:
        self.debug.add('Fullscreen')
      self.fullscreen = True
    else:
      if self.fullscreen == True:
        self.debug.add('Windowed')
      self.fullscreen = False

    self.label.draw()
    self.title.draw()
    self.reslabel.draw()


  def getRect(self, pos, image):
    return pygame.Rect((pos, self.pos[1]), (image.get_width(), image.get_height()))

  def getRes(self):
    return self.current, self.fullscreen

